package com.macro.mall.component;

import com.alibaba.fastjson.JSONObject;
import com.macro.mall.common.service.RedisService;
import com.macro.mall.common.util.DateUtil;
import com.macro.mall.dto.UmsCheckInDto;
import com.macro.mall.mapper.UmsMemberMapper;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 每天00点把当天的签到状态置为0
 * 每周一凌晨1点把周一的签到状态置为0，其余的置为2
 * @author 高强
 * @Date 2020年9月7日
 */

@Component
public class CheckInTimingTask {
    Logger logger = LoggerFactory.getLogger(CheckInTimingTask.class);
    @Autowired
    private UmsMemberMapper memberMapper;
    @Autowired
    private RedisService redisService;

    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.key.member}")
    private String REDIS_KEY_MEMBER;

    /**
     * 每天24点把当天的签到2（未到时间）置为0（未签到）
     * @author 高强
     * @Date 2020年9月7日
     */
    @Scheduled(cron = "0 0 0 * * *")
    private void setCheckInStatusOfDay() {
        UmsMemberExample example = new UmsMemberExample();
        example.setOrderByClause("id");
        List<UmsMember> list = memberMapper.selectByExample(example);
        for (UmsMember member : list) {
            String checkIn = member.getCheckIn();
            if (checkIn == null || "".equals(checkIn)) {
                //新建用户签到字段为空，根据当前周几重置
                checkIn = getNewCheckIn();
            } else {
                JSONObject jsonObject = JSONObject.parseObject(checkIn);
                UmsCheckInDto dto = JSONObject.toJavaObject(jsonObject, UmsCheckInDto.class);
                int week = DateUtil.getWeek();
                switch (week) {
                    case 1:
                        dto.setMon(0);
                        break;
                    case 2:
                        dto.setTue(0);
                        if(dto.getMon() == 0){
                            dto.setMon(2);
                        }
                        break;
                    case 3:
                        dto.setWed(0);
                        if(dto.getTue() == 0){
                            dto.setTue(2);
                        }
                        break;
                    case 4:
                        dto.setThu(0);
                        if(dto.getWed() == 0){
                            dto.setWed(2);
                        }
                        break;
                    case 5:
                        dto.setFri(0);
                        if(dto.getThu() == 0){
                            dto.setThu(2);
                        }
                        break;
                    case 6:
                        dto.setSat(0);
                        if(dto.getFri() == 0){
                            dto.setFri(2);
                        }
                        break;
                    case 7:
                        dto.setSun(0);
                        if(dto.getSat() == 0){
                            dto.setSat(2);
                        }
                        break;
                }
                jsonObject = (JSONObject) JSONObject.toJSON(dto);
                checkIn = jsonObject.toString();
            }
            member.setCheckIn(checkIn);
            memberMapper.updateByPrimaryKeySelective(member);
            String key = REDIS_DATABASE + ":" + REDIS_KEY_MEMBER + ":" + member.getUsername();
            redisService.del(key);
        }
    }

    /**
     * 每周一凌晨1点，把所有用户的签到状态改成{"mon":0,"tue":2,"wed":2,"thu":2,"fri":2,"sat":2,"sun":2}
     * @author 高强
     * @Date 2020年9月7日
     */
    @Scheduled(cron = "0 0 1 * * 1")
    private void setCheckInStatusOfWeek() {
        UmsCheckInDto dto = new UmsCheckInDto(0,2,2,2,2,2,2);
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(dto);
        String checkIn = jsonObject.toString();
        UmsMember member = new UmsMember();
        member.setCheckIn(checkIn);
        int i = memberMapper.updateByExampleSelective(member, new UmsMemberExample());
        String key = REDIS_DATABASE + ":" + REDIS_KEY_MEMBER + ":" + member.getUsername();
        redisService.del(key);
        System.out.println(i);
    }


    /**
     * 新用户checkIn字段为空，根据当前周几重置
     * @return
     */
    private String getNewCheckIn() {
        UmsCheckInDto dto;
        int week = DateUtil.getWeek();
        switch (week) {
            case 1:
                dto = new UmsCheckInDto(0, 2, 2, 2, 2, 2, 2);
                break;
            case 2:
                dto = new UmsCheckInDto(2, 0, 2, 2, 2, 2, 2);
                break;
            case 3:
                dto = new UmsCheckInDto(2, 2, 0, 2, 2, 2, 2);
                break;
            case 4:
                dto = new UmsCheckInDto(2, 2, 2, 0, 2, 2, 2);
                break;
            case 5:
                dto = new UmsCheckInDto(2, 2, 2, 2, 0, 2, 2);
                break;
            case 6:
                dto = new UmsCheckInDto(2, 2, 2, 2, 2, 0, 2);
                break;
            case 7:
                dto = new UmsCheckInDto(2, 2, 2, 2, 2, 2, 0);
                break;
            default:
                dto = new UmsCheckInDto();
        }
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(dto);
        String checkIn = jsonObject.toString();
        return checkIn;
    }


}
